#ifndef CAMERACOUNTER_H
#define CAMERACOUNTER_H

#include <QObject>
#include <QLabel>
#include "HeadCounter.h"
#include "Camera.h"
#include "VideoSource.h"
#include "WebSocketCaster.h"
#include "CarriageValue.h"
#include "CountExceptionChecker.h"
#include "HistogramWidget.h"

#include "CounterThread.h"

class CameraCounter : public QObject
{
    Q_OBJECT
public:
    explicit CameraCounter(QObject *parent = 0);

    void setupUI(QLabel* videoLabel, HistogramWidget* statusWidget);

    void setCamera(Camera::Device& dev);

    const Camera::Device& getCamera() const;

    void start();

    void stop();

    HeadCounter::Params& counterParams();

    const HeadCounter::Params& counterParams() const;

    void setRate(int fps);

    static QColor getStatusColor(int status);
	static double getStatusScale(int status);

	void processCount();

signals:
    void countResult(int index, int count, int status, bool exception);

protected:
    virtual void timerEvent(QTimerEvent *event);

public slots:
	void onCounterDone();

private:

private:
    void handleFrameTimer();

    void showImage(cv::Mat& mat);

    void showImage(cv::Mat& mat, QLabel* label);

    void buildVideoSource();

    void broadcast(cv::Mat& mat, int count);

    void loadCarriageValueConfig();

    void showStatus(int count);



private:
    Camera::Device  m_device;

    //cv::Ptr< cv::VideoCapture> m_videoCapture;
    cv::Ptr< VideoSource >	m_videoSource;

    int m_timerId;

    HeadCounter	m_headCounter;

    int	m_rate;

    WebSocketCaster	m_caster;

    CarriageValue   m_carriageValue;
    CountExceptionChecker   m_exceptionChecker;

    QLabel*     m_videoLabel;
    HistogramWidget*    m_statusWidget;

	cv::Mat	m_outFrame;
	int	m_count;
	comn::CriticalSection	m_cs;

	CounterThread*	m_counterThread;

};

#endif // CAMERACOUNTER_H
